package com.cgly.medical.controller;

import com.cgly.medical.domain.Account;
import com.cgly.medical.domain.Doctor;
import com.cgly.medical.domain.Drug;
import com.cgly.medical.service.DrugService;
import com.cgly.medical.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangxiangli
 * @create 2022-08-19-9:19
 */
@Api(description = "药品信息管理")
@RestController
@RequestMapping("/drugService")
@CrossOrigin
public class DrugController {

    @Autowired
    private DrugService drugService;

    //分页查询
    @PostMapping("getDrugInfoByPage/{current}/{limit}")
    public Msg getDrugInfoByPage(@PathVariable int current,
                               @PathVariable int limit,
                               @RequestBody(required = false) Drug drug){

        PageHelper.startPage(current,limit);
        List<Drug> drugList = drugService.getDrugInfoByPage(drug);
//  PageInfo<Doctor> doctorPageInfo=new PageInfo<Doctor>(doctors,limit);
        PageInfo<Drug> doctorPageInfo1=new PageInfo<Drug>(drugList,limit);
        List<Drug> list = doctorPageInfo1.getList();
        long total = doctorPageInfo1.getTotal();
        if(drugList==null){
            return Msg.fail();
        }
        return Msg.success().data("total",total).data("rows",list);
    }

    //通过id进行查询
    @GetMapping("selectDrugInfoByid/{id}")
    public Msg selectDrugInfoByid(@PathVariable String id){
        Drug drug = drugService.selectDrugByid(id);
        if (drug==null){
            return Msg.fail();
        }
        return Msg.success().data("rows",drug);
    }


    //修改方法
    @PostMapping("updateDrugInfo")
    public Msg updateDrugInfo(@RequestBody Drug drug){
        int i = drugService.updateDrugInfo(drug);
        if(i==0){
            return Msg.fail();
        }
        return Msg.success();
    }

    //删除方法
    @DeleteMapping("deleteDrugInfo/{id}")
    public Msg deleteDrugInfo(@PathVariable  String id){
        int i = drugService.deleteDruginfo(id);
        if(i==0){
            return Msg.fail();
        }
        return Msg.success();
    }

    //添加药品信息
    @PostMapping("addDruginfo")
    public Msg addDruginfo(@RequestBody Drug drug){

       int i=drugService.addDruginfo(drug);
       if(i==0){
           return Msg.fail();
       }
        return Msg.success();

    }
}
